using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.UI;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace WOODIFramework.Model
{
    [ActiveRecord("Label")]
    public class Label : PageItem
    {
        #region Fields

        private Guid _labelId;
        private string _tooltip;
        private string _labelText;
        private int _headerLevel = -1;

        #endregion

        #region Properties

        [JoinedKey("labelID")]
        public Guid LabelId
        {
            get { return _labelId; }
            set { _labelId = value; }
        }

        [Property]
        public string LabelText
        {
            get { return _labelText; }
            set { _labelText = value; }
        }

        [Property]
        public string Tooltip
        {
            get { return _tooltip; }
            set { _tooltip = value; }
        }

        [Property]
        public int HeaderLevel
        {
            get { return _headerLevel; }
            set
            {
                if (value == 0 | value > 4)
                {
                    _headerLevel = -1;
                }
                else
                { _headerLevel = value; }
            }

        }

        #endregion

        #region Constructors

        private Label()
        {
            this.Id = Guid.NewGuid();
            this.LabelId = this.Id;
        }


        private Label(string Name)
        {
            this.Id = Guid.NewGuid();
            this.LabelId = this.Id;
            this.Name = Name;
        }

        private Label(string Name, string LabelText)
        {
            this.Id = Guid.NewGuid();
            this.LabelId = this.Id;
            this.Name = Name;
            this.LabelText = LabelText;
        }

        #endregion

        #region Public Methods

        #region Public Static Methods

        public static Label CreateLabel()
        {
            return new Label();
        }

        public static Label CreateLabel(string Name)
        {
            return new Label(Name);
        }

        public static Label CreateLabel(string Name, string LabelText)
        {
            return new Label(Name, LabelText);
        }

        #endregion

        public string RenderHTML()
        {
            StringBuilder stringBuilder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(stringBuilder);
            HtmlTextWriter writer = new HtmlTextWriter(stringWriter);

            writer.WriteBeginTag("div");
            writer.WriteAttribute("id", "label" + Id.ToString());
            writer.WriteAttribute("name", Name);
            writer.Write(HtmlTextWriter.TagRightChar);

            writer.WriteBeginTag("span");
            writer.WriteAttribute("id", Id.ToString());
            writer.WriteAttribute("name", Name);
            writer.WriteAttribute("title", Tooltip);
            writer.Write(HtmlTextWriter.TagRightChar);

            if (_headerLevel != -1)
            { writer.WriteFullBeginTag("h" + _headerLevel.ToString()); }
            if (DataBoundObjectProperties != null)
            {
                if (DataBoundObjectProperties.Count > 0)
                {
                    if (DataBoundFieldsFormatString != string.Empty)
                    {

                    }
                    else
                    {
                        string[] keys = new string[DataBoundObjectProperties.Keys.Count];
                        string[] values = new string[DataBoundObjectProperties.Values.Count];
                        DataBoundObjectProperties.Keys.CopyTo(keys, 0);
                        DataBoundObjectProperties.Values.CopyTo(values, 0);
                        Object requiredObject = BoundObjects[keys[0]];
                        PropertyInfo propertyInfo = requiredObject.GetType().GetProperty(values[0]);
                        string returnedValue = propertyInfo.GetValue(requiredObject, null).ToString();
                        writer.Write(returnedValue);
                    }
                }
                else
                { writer.Write(LabelText); }
            }
            else
            { writer.Write(LabelText); }

            if (_headerLevel != -1)
            { writer.WriteEndTag("h" + _headerLevel.ToString()); }
            writer.WriteEndTag("span");
            writer.WriteEndTag("div");
            writer.WriteLine();

            return stringWriter.ToString();
        }

        #endregion
    }
}
